package com.selfdriving.wl.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.selfdriving.BaseApplication;
import com.selfdriving.BaseFragment;
import com.selfdriving.R;
import com.selfdriving.adapter.WMyGroupsAdapter;
import com.selfdriving.bean.JsonResolveUtils;
import com.selfdriving.view.MoMoRefreshListView;
import com.selfdriving.view.MoMoRefreshListView.OnCancelListener;
import com.selfdriving.view.MoMoRefreshListView.OnRefreshListener;

@SuppressLint("ValidFragment")
public class MyGrpupsFragment extends BaseFragment implements
		OnItemClickListener, OnRefreshListener, OnCancelListener
{

	private MoMoRefreshListView mMmrlvList;
	private WMyGroupsAdapter mAdapter;

	public MyGrpupsFragment()
	{
		super();
	}

	public MyGrpupsFragment(BaseApplication application, Activity activity,
			Context context)
	{
		super(application, activity, context);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		mView = inflater.inflate(R.layout.w_activity_near_service_vip,
				container, false);
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	protected void initViews()
	{
		mMmrlvList = (MoMoRefreshListView) findViewById(R.id.w_near_service_hotel_list);
		mMmrlvList.removeFooterView(mMmrlvList.mFootView);
	}

	@Override
	protected void initEvents()
	{
		mMmrlvList.setOnItemClickListener(this);
		mMmrlvList.setOnRefreshListener(this);
		mMmrlvList.setOnCancelListener(this);
	}

	@Override
	protected void init()
	{
		getPeoples(true);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	{
	}

	private void getPeoples(final boolean showText)
	{

		putAsyncTask(new AsyncTask<Void, Void, Boolean>()
		{

			@Override
			protected void onPreExecute()
			{
				super.onPreExecute();
				if (showText)
				{
					showLoadingDialog("正在加载,请稍后...");
				}
			}

			@Override
			protected Boolean doInBackground(Void... params)
			{
				return JsonResolveUtils.resolveMyGroups(mApplication);
			}

			@Override
			protected void onPostExecute(Boolean result)
			{
				super.onPostExecute(result);
				dismissLoadingDialog();
				if (!result && showText)
				{
					showCustomToast("数据加载失败...");
				} else
				{

					mAdapter = new WMyGroupsAdapter(mApplication, mContext,
							mApplication.myGroupsList);
					mMmrlvList.setAdapter(mAdapter);

				}
			}
		});
	}

	@Override
	public void onCancel()
	{
		clearAsyncTask();
		mMmrlvList.onRefreshComplete();
	}

	@Override
	public void onRefresh()
	{
		putAsyncTask(new AsyncTask<Void, Void, Boolean>()
		{

			@Override
			protected Boolean doInBackground(Void... params)
			{
				try
				{
					Thread.sleep(1500);
					getPeoples(false);
				} catch (InterruptedException e)
				{

				}
				return null;
			}

			@Override
			protected void onPostExecute(Boolean result)
			{
				super.onPostExecute(result);
				mMmrlvList.onRefreshComplete();
			}
		});
	}

	public void onManualRefresh()
	{
		mMmrlvList.onManualRefresh();
	}

	@Override
	public void onLoadMore()
	{
		// TODO Auto-generated method stub

	}
}
